<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/market/marketingPlay' }"> <i class="el-icon-lx-cascades"></i> 营销玩法 </el-breadcrumb-item>
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 优惠券 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增优惠券</el-button>
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="输入优惠券名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="name" label="优惠券名称"></el-table-column>
                <el-table-column label="类型">
                    <template slot-scope="scope">
                        <!-- <span class="red">{{ scope.row.manjian_menkan_amount > 0 ? '	满减卷' : '无门槛卷' }}</span> -->
						<span class="red">{{ scope.row.type == 0 ? '满减优惠券' : '折扣优惠券' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="优惠内容">
                    <template slot-scope="scope">
                        {{
                            scope.row.type == 0
								? (scope.row.manjian_menkan_amount > 0 
									? '满' + scope.row.manjian_menkan_amount + '减' + scope.row.manjian_amount
									: '无门槛,减' + scope.row.manjian_amount
								)
									
								: (scope.row.zhekou_more_amount > 0 
									? (scope.row.zhekou_menkan_amount > 0 
										? '打' + scope.row.zhekou_num + '折' + ',最多优惠￥' + scope.row.zhekou_more_amount
										: '无门槛,打' + scope.row.zhekou_num + '折' + ',最多优惠￥' + scope.row.zhekou_more_amount
									)
									: '打' + scope.row.zhekou_num + '折'
								)
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="已领取/优惠券总量">
                    <template slot-scope="scope">
                        {{ scope.row.used_number + '/' + scope.row.number }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="use_number" label="已使用"></el-table-column> -->
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button @click="edit(scope.row.id, scope.row)" type="success" size="mini">编辑</el-button>
                            <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger" size="mini">删除</el-button>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增优惠券弹出框 -->
        <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="优惠券名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
				
				<el-form-item label="优惠券类型">
					<el-radio v-model="form.type" :label="0">满减优惠券</el-radio>
					<el-radio v-model="form.type" :label="1">折扣优惠券</el-radio>
				</el-form-item>
				
                <el-form-item label="满减使用门槛" v-if="form.type==0">
                    <el-radio-group v-model="manjian_menkan_status" @input="onManJianMenKanInput">
                        <el-radio :label="0">无门槛使用</el-radio>
                        <el-radio :label="1">
                            <span
                                >满<el-input
                                    v-model="form.manjian_menkan_amount"
                                    style="width: 80px; margin: 0px 10px"
                                    size="mini"
                                    placeholder="输入金额"
									:disabled = "manjian_menkan_status == 0 ? true : false"
                                ></el-input
                                >元使用</span
                            >
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
				
                <el-form-item label="满减优惠内容" v-if="form.type==0">
                    <span
                        >减<el-input
                            v-model="form.manjian_amount"
                            style="width: 120px; margin: 0px 10px"
                            size="mini"
                            placeholder="输入优惠券金额"
                        ></el-input
                        >元</span
                    >
                </el-form-item>
				
				<el-form-item label="消费折扣" v-if="form.type==1">
					打 <el-select style="width: 85px" size="mini" v-model="form.zhekou_num" clearable>
					    <el-option v-for="item in zhekouOptions" :key="item" :value="item"></el-option>
					</el-select>  折
					<!-- 
					<el-checkbox :indeterminate="indeterminate" :checked="checkAll"   @change="onCheckAllChange"> -->
					<el-checkbox :checked="form.zhekou_more_check" @change="onCheckZheKouMoreChange">
						最多优惠
						<el-input
					                v-model="form.zhekou_more_amount"
					                style="width: 80px; margin: 0px 10px"
					                size="mini"
					                placeholder="输入金额"
									:disabled = "form.zhekou_more_check == 0 ? true : false">
									<!-- disabled -->
						</el-input>
					</el-checkbox>
					
				</el-form-item>
				
				
				<el-form-item label="使用门槛" v-if="form.type==1">
					<el-radio-group v-model="zhekou_menkan_status" @input="onZheKouMenKanInput">
					    <el-radio :label="0">无门槛使用</el-radio>
					    <el-radio :label="1">
					        <span
					            >满<el-input
					                v-model="form.zhekou_menkan_amount"
					                style="width: 80px; margin: 0px 10px"
					                size="mini"
					                placeholder="输入金额"
									:disabled = "zhekou_menkan_status == 0 ? true : false"
					            ></el-input
					            >元使用</span
					        >
					    </el-radio>
					</el-radio-group>
				</el-form-item>
				
				
				
                <el-form-item label="发行量">
                    <span
                        ><el-input
                            v-model="form.number"
                            style="width: 100px; margin-right: 10px"
                            size="mini"
                            placeholder="最多1000张"
                        ></el-input
                        >张</span
                    >
                </el-form-item>
				
				<el-form-item label="适用商品">
					<el-radio v-model="menkan_status" :label="0">全部商品可用</el-radio>
					<el-radio v-model="menkan_status" :label="1">指定商品可用</el-radio>
				</el-form-item>
				
                <el-form-item label="">
                    <el-button @click="chooseCourse" type="primary" size="small">选择课程</el-button>
                    <el-table
                        :data="selectRowData"
                        v-if="selectRowData.length !== 0"
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                    >
                        <el-table-column label="商品信息" width="250">
                            <template slot-scope="scope">
                                <div class="course-info-wrapper">
                                    <div class="img-wrapper">
                                        <img :src="scope.row.cover" alt="" class="info-image-mini" />
                                    </div>
                                    <div class="info-detail">
                                        <div class="title">
                                            <span>{{ scope.row.title }}</span>
                                        </div>
                                        <span>{{ scope.row.description }}</span>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="类型">
                                        <template slot-scope="scope">
                                            <span>{{
                                                scope.row.type == 'image_text'
                                                    ? '图文'
                                                    : scope.row.type == 'audio'
                                                    ? '音频'
                                                    : scope.row.type == 'video'
                                                    ? '视频'
                                                    : '专栏'       
                                            }}</span>
                                        </template>
                                    </el-table-column>
                        <el-table-column label="价格">
                            <template slot-scope="scope">
                                <span class="red">{{ '¥ ' + scope.row.price }}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
				
				<el-form-item label="使用时间">
				    <el-date-picker
				        v-model="useTimeDate"
				        type="datetimerange"
				        range-separator="至"
				        start-placeholder="开始日期"
				        end-placeholder="结束日期"
				        value-format="yyyy-MM-dd HH:mm:ss"
				    >
				    </el-date-picker>
				</el-form-item>
				
                <el-form-item label="领取次数限制">
                    <el-radio-group v-model="ling_status">
                        <el-radio :label="true">不限次数</el-radio>
                        <el-radio :label="false">
                            <span>
                                每人领取
                                <el-select style="width: 85px" size="mini" v-model="form.ling_num" clearable>
									<el-option v-for="item in options" :key="item" :value="item"></el-option>
								</el-select>
                                次
                            </span>
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="分享设置">
                    <el-switch
                        v-model="form.is_fenxiang"
                        :active-value="1"
                        :inactive-value="0"
                        active-text="优惠券允许分享给好友领取"
                    ></el-switch>
                </el-form-item>
                <el-form-item label="公开设置">
                    <el-switch
                        v-model="form.is_gongkai"
                        :active-value="1"
                        :inactive-value="0"
                        active-text="用户可在商品详情页领取"
                    ></el-switch>
                </el-form-item>
                <!-- <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 选择产品类型 -->
        <el-dialog width="30%" title="选择产品类型" :visible.sync="productTypeVisible" append-to-body>
            <div class="product_type">
                <span v-for="item in productType" :key="item.id" @click="chooseType(item.value)">
                    {{ item.label }}
                </span>
            </div>
        </el-dialog>
        <!-- 打开内层弹框 -->
        <el-dialog width="36%" :title="`选择${productTypeTitle}`" @close="exit" :visible.sync="innerVisible" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item>
                    <el-input v-model="query.keyword" :placeholder="`输入${productTypeTitle}名称`" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="courseSearch">搜索</el-button>
                </el-form-item>
                <el-table
                    :data="chooseCourseTableData"
                    @row-click="chooseRow"
                    class="chooseCourseTable"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                >
                    <el-table-column :label="`${productTypeTitle}封面`">
                        <template slot-scope="scope">
                            <img :src="scope.row.cover" alt="" class="info-image" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="title" :label="`${productTypeTitle}名称`"></el-table-column>
                    <el-table-column label="免费/付费">
                        <template slot-scope="scope">
                            <span style="color: #e65c5c">{{ scope.row.price == 0 ? '免费' : '¥ ' + scope.row.price }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="type" :label="`${productTypeTitle}类型`" v-if="productTypeTitle == '课程'">
                        <template slot-scope="scope">
                            {{ scope.row.type == 'video' ? '视频' : scope.row.type == 'audio' ? '音频' : '图文' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="状态">
                        <template slot-scope="scope">
                            {{ scope.row.status == 0 ? '未上架' : '已上架' }}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.page"
                        :page-size="query.rows"
                        :total="courseListTotal"
                        @current-change="coursePageChange"
                    ></el-pagination>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { couponListData, videoListData, columnListData, examListData, addCouponData, delCouponData, couponListDataNew} from '../../api/index';
export default {
    // name: 'lecturer',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
			form: {
				id: '',
				name: '',
				type: '',
				manjian_amount: '',
				manjian_menkan_amount: '',
				zhekou_num: '',
				zhekou_menkan_amount: '',
				zhekou_more_amount: '',
				zhekou_more_check: '',
				
				
				number: '',
				used_number: '',
				use_type: '',
				use_good: '',
				start_time: '',
				end_time: '',
				is_share: '',
				is_public: '',
				status: '',
			},
			
            // form: {
            //     id: '',
            //     k_id: '',
            //     info_type: '',
            //     name: '',
            //     menkan_price: '',
            //     jian_price: '',
            //     number: '',
            //     start_time: '',
            //     end_time: '',
            //     ling_num: '',
            //     is_fenxiang: '',
            //     is_gongkai: ''
            //     // status: '',
            // },
            productType: [
                {
                    value: 'knowledge',
                    label: '图文'
                },
                {
                    value: 'knowledge',
                    label: '音频'
                },
                {
                    value: 'knowledge',
                    label: '视频'
                },
                {
                    value: 'column',
                    label: '专栏'
                }
            ],
			manjian_menkan_status: '',
			zhekou_menkan_status: '',
			
			load_zhekou_more_amount:'',
			load_manjian_menkan_amount:'',
			
			// zhekou_more_check: false,
			
			
            menkan_status: '',
            ling_status: '',
            useTimeDate: [],
            tableData: [],
			zhekouOptions: [1, 2, 3, 4, 5, 6, 7, 8, 9],
            options: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            chooseCourseTableData: [],
            selectRowData: [],
            productTypeTitle: '',
            showDialog: false,
            productTypeVisible: false,
            innerVisible: false,
            pageTotal: 0,
            courseListTotal: 0
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            couponListDataNew(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
				
				// console.log(res.data)
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        // 课程分页
        coursePageChange(val) {
            this.$set(this.query, 'page', val);
            this.courseSearch();
        },
        //新增
        addDialog() {
            this.showDialog = true;
            this.selectRowData = [];
        },
        // 课程搜索
        courseSearch() {
            if (this.productTypeTitle == '课程') {
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else if (this.productTypeTitle == '专栏') {
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                examListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseCourse() {
            this.productTypeVisible = true;
        },
        chooseType(type) {
            this.innerVisible = true;
            this.query.page = 1;
            this.query.keyword = '';
            if (type == 'knowledge') {
                this.productTypeTitle = '课程';
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                this.productTypeTitle = '专栏';
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseRow(row) {
            this.selectRowData = [];
            this.selectRowData.push(row);
            this.form.k_id = row.id;
            if (row.type == 'column') {
                this.form.info_type = 'column';
            } else {
                this.form.info_type = 'knowledge';
            }
            this.innerVisible = false;
            this.productTypeVisible = false;
        },
		//满减优惠券 使用门槛	input事件
		onManJianMenKanInput(i){
			if(i==0){
				this.form.manjian_menkan_amount=0;
			}else{
				this.form.manjian_menkan_amount=this.load_manjian_menkan_amount;
			}
		},
		
		//折扣优惠券 最多优惠 CheckBox	change事件
		onCheckZheKouMoreChange(i){
			if(i==false){
				this.form.zhekou_more_check=0;
				this.form.zhekou_more_amount=0;
			}else{
				this.form.zhekou_more_check=1;
				this.form.zhekou_more_amount=this.load_zhekou_more_amount;
			}
		},
		//折扣优惠券 使用门槛	input事件
		onZheKouMenKanInput(i){
			if(i==0){
				this.form.zhekou_menkan_amount=0;
			}else{
				this.form.zhekou_menkan_amount=this.load_zhekou_menkan_amount;
			}
		},
        //确定
        save() {
            this.form.start_time = this.useTimeDate[0];
            this.form.end_time = this.useTimeDate[1];
            addCouponData(this.form).then((res) => {
                if (res.code == 1) {
                    this.showDialog = false;
                    this.$message.success('操作成功');
                    this.getData();
                }
            });
        },
        //编辑
        edit(id, row) {
			// console.log(row);
			this.load_zhekou_more_amount = row.zhekou_more_amount;
			this.load_zhekou_menkan_amount = row.zhekou_menkan_amount;
			this.load_manjian_menkan_amount = row.manjian_menkan_amount;
			
            this.selectRowData = [];
            this.showDialog = true;
            this.form.id = id;
            this.form = row;
            this.selectRowData.push(row.info);
			
			// console.log(this.form);
			// zhekou_more_check
			
			// 满减
			if(this.form.manjian_menkan_amount == 0){
				this.manjian_menkan_status = 0;
				this.form.manjian_menkan_amount = '';
			}else{
				this.manjian_menkan_status = 1;
			}
			
			// 折扣
			if(this.form.zhekou_menkan_amount == 0){
				this.zhekou_menkan_status = 0;
				this.form.zhekou_menkan_amount = '';
			}else{
				this.zhekou_menkan_status = 1;
			}
			
			// 折扣 最多优惠
			if(this.zhekou_more_check == 0){
				this.zhekou_more_check = false;
			}else{
				this.zhekou_more_check = true;
			}
			
			
            // if(row.menkan_price == 0) {
            //     this.menkan_status = 0;
            //     this.form.menkan_price = '';
            // } else {
            //     this.menkan_status = 1;
            // }
			
            if(row.ling_num == 0) {
                this.ling_status = true;
                this.form.ling_num = '';
            } else {
                this.ling_status = false;
            }
            this.useTimeDate = [row.start_time, row.end_time];
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delCouponData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.getData();
                    }
                });
            });
        },
        //取消
        cancel() {
            this.showDialog = false;
            this.form = {};
        },
        //关闭
        exit() {
            this.innerVisible = false;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
.innerPagination {
    float: left;
}
.dialog-footer {
    display: block;
}

/* 授权课程start */
.course-info-wrapper {
    display: flex;
    align-items: center;
    width: 100%;
    height: 64px;
    text-align: left;
    cursor: pointer;
}
.course-info-wrapper .img-wrapper {
    position: relative;
}
.info-image {
    height: 64px;
    width: 80px;
    border-radius: 4px;
}
.info-image-mini {
    height: 60px;
    width: 60px;
    border-radius: 50%;
}
.red {
    color: #ea644a;
}
.course-info-wrapper .info-detail {
    line-height: 1.5;
    height: 100%;
    margin-left: 8px;
}
.course-info-wrapper .info-detail .title {
    max-width: 100%;
    height: 44px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    white-space: pre-wrap;
}
.course-info-wrapper .info-detail .price {
    font-size: 14px;
    font-weight: 400;
    color: #e65c5c;
}
.product_type {
    text-align: center;
}
.product_type span {
    cursor: pointer;
    display: inline-block;
    padding: 0 5px;
    margin: 0 20px;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 3px;
}
.chooseCourseTable {
    cursor: pointer;
}
/* 授权课程end */
</style>


